#include "sendfile.h"
#include <QFile>
#include <QFileInfo>
#include <QDebug>
#include <QProgressDialog>

SendFile::SendFile(QObject *parent) : QObject(parent)
{

}

void SendFile::sendFile(QString path)
{
    QFile file(path);
    QFileInfo info(path);
    QString fileName = info.fileName();
    int fileSize = info.size();
    emit sendFileContent(fileName.size(), fileName);
    file.open(QFile::ReadOnly);
    static int num = 0;
    QProgressDialog *dlg=new QProgressDialog( QStringLiteral("正在传输......"), QStringLiteral("取消"),0,100,NULL);
    dlg->setWindowTitle(tr("文件传输"));
    dlg->setWindowModality(Qt::WindowModal);
    dlg->show();
    while (!file.atEnd()) {
        QByteArray line = file.readLine();
        //发送data
        QString data = line;
        emit sendFileContent(line.size(), data);
        num += line.size();
        int percent = (num * 100 / fileSize);
        //发送进度
        dlg->setValue(percent);
    }
    dlg->setValue(100);
    emit over();
}
